﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Xml;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using OgilvyOne.Model;
using OgilvyOne.BLL;

/// <summary>
/// 后台管理程序Page基类,判断登录及权限
/// </summary>

public class AdminBasePage : BasePage
{
    public string layerid;
    protected override void OnLoad(EventArgs e)
    {

        if (GetSessionUsersModel() == null)
        {
            //string path = Request.Path;
            Response.Redirect( path + "/admin/login.aspx");
            return;

        }
        layerid = Request.Params["layerid"];
        base.OnLoad(e);
        //PopedomValidate(layerid, QUERY);
    }

    public string Layerid
    {
        get { return layerid; }
    }
    
    public AdminBasePage()
    {
        
    }

    protected const string ADD = "add";
    protected const string MOD = "mod";
    protected const string DEL = "del";
    protected const string QUERY = "show";
    protected const string AUDIT = "audit";
    protected const string PUB = "pub";
    /// <summary>
    /// 后台栏目权限验证
    /// </summary>
    /// <param name="layerid">栏目层级ID</param>
    /// <param name="operation">操作类型 (ADD,MOD,DEL,QUERY)</param>
    public void PopedomValidate(string layerid, string operation)
    {
        string path = Request.ApplicationPath;
        if (path == "/")
        {
            path = "";
        }
        if (layerid == null || layerid == "")
        {
            Response.Redirect(path + "/admin/popedom_error.aspx?t=1", true);
            return;
        }
        if (!PopedomValidateHtml(layerid, operation))
        {
            Response.Redirect(path + "/admin/popedom_error.aspx", true);
        }
    }

    public string path
    {
        get { 
            string path = Request.ApplicationPath;
            if (path == "/")
            {
                path = "";
            }
            return path;        
        }
    }

    /// <summary>
    /// 获取当前后台登录人信息
    /// </summary>
    /// <returns></returns>
    public  UsersModel GetSessionUsersModel()
    {
        //return (UsersModel)Session[OgilvyOne.BLL.LoginBll.LOGIN_USER_KEY];
        CmsUserSessionBll bll = new CmsUserSessionBll();
        return bll.GetUserFromSession();
    }

    public bool PopedomValidateHtml(string layerid, string operation)
    {
        if (layerid == null || layerid == "") return false;
        if (!isDynamicPopedom())
        {
            bool isp = false;
            Hashtable map = (Hashtable)Session[OgilvyOne.BLL.LoginBll.LOGIN_USER_POPEDOM_KEY];
            if (map != null)
            {
                if (map.ContainsKey(layerid))
                {
                    string popedom = (string)map[layerid];
                    if (popedom.IndexOf(operation) > -1)
                    {
                        isp = true;
                    }
                    else
                    {
                        if (operation == QUERY)
                        {
                            isp = true;
                        }
                    }
                }
            }
            return isp;
        }
        return true;
    }

    public bool isDynamicPopedom()
    {
        OgilvyOne.BLL.LoginBll login = new OgilvyOne.BLL.LoginBll();
        return login.isDynamicPopedom(Session);
    }

    public string setbBckUrl(string backurl)
    {
        if (backurl != null)
        {
            if (backurl.IndexOf("?") > -1)
            {
                backurl += "&";
            }
            else
            {
                backurl += "?";
            }
            backurl += "layerid=" + layerid;
        }
        return backurl;
    }

    /// <summary>
    /// 插入操作日志 
    /// </summary>
    /// <returns></returns>
    public void insertLog(string logcontent)
    {
        //int state = switchLog();
        //if (state == 0)
        //{
        //    string user = GetSessionUsersModel().Aid.ToString();
        //    DateTime dt = DateTime.Now;
        //    LogManageBLL log = new LogManageBLL();
        //    LogManage loginfo = new LogManage();
        //    loginfo.Memberid = user.Trim();
        //    loginfo.Logcontent = logcontent.Trim();
        //    loginfo.Executetime = dt;
        //    log.insert(loginfo);
        //}
        //else
        //{ 
        //}
    }
    /// <summary>
    /// 操作日志状态查询 
    /// </summary>
    /// <returns></returns>
    private int switchLog()
    {
        int state = 0;
        LogManageBLL lmb = new LogManageBLL();
        LogSwitchs ls = lmb.selectLogsSwitch();
        state = int.Parse(ls.Switchs.ToString());
        return state;
    }
    /// <summary>
    /// 分页HTML
    /// </summary>
    /// <param name="pageCount"></param>
    /// <param name="pageNum"></param>
    /// <param name="totalCount"></param>
    /// <returns></returns>
    public string getPageHtml(int pageCount, int pageNum, int totalCount)
    {
        StringBuilder html = new StringBuilder();

        if (totalCount > 0)
        {
            int nextPage = pageNum + 1 > pageCount ? pageCount : pageNum + 1;
            int prevPage = pageNum - 1 < 1 ? 1 : pageNum - 1;

            html.Append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\">");
            html.Append("<tr>");
            html.Append("<td height=\"30\" align=\"center\">");
            html.Append("第 <b>" + pageNum + "</b>/<b>" + pageCount + "</b> 页 ");
            html.Append("总记录 <b>" + totalCount + "</b>&nbsp;&nbsp;&nbsp;");
            //
            if (pageNum == 1)
            {
                html.Append("首页 上一页");
            }
            else
            {
                html.Append("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=1;document.forms[0].submit();return false;\">首页</a>");
                html.Append(" <a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=" + prevPage + ";document.forms[0].submit();return false;\" >上一页</a> ");
            }
            if (pageNum >= pageCount)
            {
                html.Append("下一页  末页");
            }
            else
            {
                html.Append("<a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=" + nextPage + ";document.forms[0].submit();return false;\" >下一页</a>");
                html.Append(" <a href=\"javascript:void(0);\" onclick=\"document.forms[0].pageNum.value=" + (pageCount) + ";document.forms[0].submit();return false;\">末页</a>");
            }
            html.Append("&nbsp;<input name=\"pageNum\" type=\"text\" style=\"HEIGHT: 18px; WIDTH: 22px;\" maxlength=\"4\" value=\"" + pageNum + "\" />");
            html.Append("<input name=\"Go\" type=\"button\" value=\"Go\" onclick=\"javascript:document.forms[0].submit();return false;\" style=\"HEIGHT: 18px; WIDTH: 22px;\" />");
            html.Append("</td>");
            html.Append("</tr>");
            html.Append("</table>");
        }
        else
        {
            html.Append("<input type=\"hidden\" id=\"pageNum\" name=\"pageNum\" value=\"" + pageNum + "\" />");
        }

        return html.ToString();
    }
    /// <summary>
    /// arg1 是否在 arg2 菜单下
    /// </summary>
    /// <param name="arg1"></param>
    /// <param name="agr2"></param>
    /// <returns></returns>
    public bool isExist(string arg1, string arg2)
    {
        string[] ss = arg2.Split(';');
        
            foreach (string str in ss)
            {
                if (arg1.Split(',').Length >= str.Split(',').Length)
                {
                    if (arg1.IndexOf(str) > -1)
                    {
                        return true;
                    }
                }
            }
        
        return false;
    }
}
